<?php

namespace backend\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

/**
 * This is the model class for table "goods".
 *
 * @property integer $id
 * @property string $name
 * @property string $sn
 * @property string $logo
 * @property string $goods_category_id
 * @property integer $brand_id
 * @property string $market_price
 * @property string $shop_price
 * @property integer $stock
 * @property string $is_on_sale
 * @property integer $status
 * @property integer $sort
 * @property integer $inputtime
 * @property integer $update_at
 */
class Goods extends ActiveRecord
{
    public static $statusValue=[1=>'正常',0=>'回收站'];

    public static $saleStatus=[1=>'是',0=>'否'];

    public $uploadFile;

    public $content;

    public $amount;

    public $total;


    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => ['inputtime','update_at'],
                'updatedAtAttribute' => 'update_at',
//                'value' => new Expression('current_timestamp'),
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'goods';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name','goods_category_id', 'brand_id','market_price', 'shop_price', 'stock', 'status', 'sort','is_on_sale'],'required'],
            [['goods_category_id', 'brand_id', 'stock', 'status', 'sort', 'inputtime', 'update_at'], 'integer'],
            [['market_price', 'shop_price'], 'number'],
            [['name'], 'string', 'max' => 50],
            [['sn'], 'string', 'max' => 15],
            [['logo'], 'string', 'max' => 150],
            [['is_on_sale'], 'string', 'max' => 4],
            [['uploadFile'],'image'],
//            [['content'],'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            //'id' => 'ID',
            'id' => '商品ID',
            'name' => '商品名称',
            'sn' => '货号',
            'logo' => '商品LOGO',
            'goods_category_id' => '商品分类',
            'brand_id' => '品牌',
            'market_price' => '市场价格',
            'shop_price' => '本店价格',
            'stock' => '库存',
            'is_on_sale' => '是否上架',
            'status' => '商品状态',
            'sort' => '排序',
            'inputtime' => '录入时间',
            'update_at' => '更新时间',
            'uploadFile'=>'LOGO',
//            'content'=>'简介',
        ];
    }

    /**
     * @return string 返回当前的商品的logo的地址
     */
    public function imgsrc(){
        $logopath =$this->logo;
        if(substr($logopath,0,4)=='http'){
            return $logopath;
        }elseif(empty($logopath)){
            return $logopath;
        }else{
            return '@web'.$logopath;
        }
    }

    public function logoUrl(){
        $logopath =$this->logo;
        if(substr($logopath,0,4)=='http'){
            return $logopath;
        }elseif(empty($logopath)){
            return $logopath;
        }else{
            return Yii::$app->params['imgUrl'].$logopath;
        }
    }


    /*
     * @intro 生成当前的商品的货号
     */
    public function createSn(){
        //获取今天的添加商品的个数的数据
        $date =date('ymd');
        //构造sql语句 如果存在更新数据,不存在添加数据
        $sql = "insert into `goods_day_count` set `day`='{$date}',`count`='1' on duplicate key update count=count+1";
        //执行sql语句
        \Yii::$app->db->createCommand($sql)->execute();
        //获取当前的是第几个添加的商品
        $count = GoodsDayCount::findOne(['day'=>$date])->count;
        //拼接商品的货号
        $this->sn=$date.str_pad($count,'5','0',STR_PAD_LEFT);
    }

    /**
     * @return array|string|ActiveRecord[] 静态方法放回所有的分类的信息(json格式的字符串)
     */
    public static function categorysInfo($depth=2){
        //获取所有的分类的数据
        $categorys = GoodsCategory::find()->where(['depth'=>$depth])->asArray()->all();
        $categorys=Json::encode($categorys);
        return $categorys;
    }
    /**
     * @return array 静态方法获取所有的商标的数据(是一个一维数组)
     */
    public static function brandsInfo(){
        //获得所有的商标的数据
        $brands = (new Query())->select('*')->from('brand')->all();
        $brands = ArrayHelper::map($brands,'id','name');
        return $brands;
    }


    /**
     * @return array 返回所有的分类的数组
     */
    public static function categorysAll(){
        //获得所有的分类的数据
        $categorys = (new Query())->select('*')->from('goods_category')/*->where(['depth'=>3])*/->all();
        $categorys = ArrayHelper::map($categorys,'id','name');
        return $categorys;
    }



    public function getGoodsIntro(){
        $rs= $this->hasOne(GoodsIntro::className(),['goods_id'=>$this->id]);
        return $rs;
    }


}
